package com.demo.study.algorithm.leetcode;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * LT412
 * <a href="https://leetcode.cn/problems/fizz-buzz/description/">Fizz Buzz</a>
 *
 * @author Baymax
 * @since 2024-11-15
 */
public class LT412 {
    @Test
    public void run() {
        System.out.println(fizzBuzz(15));
        System.out.println(fizzBuzz2(15));
    }

    public List<String> fizzBuzz(int n) {
        List<String> result = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0) {
                result.add("FizzBuzz");
            } else if (i % 3 == 0) {
                result.add("Fizz");
            } else if (i % 5 == 0) {
                result.add("Buzz");
            } else {
                result.add(i + "");
            }
        }
        return result;
    }

    public List<String> fizzBuzz2(int n) {
        List<String> result = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            String txt = "";
            if (i % 3 == 0) {
                txt += "Fizz";
            }
            if (i % 5 == 0) {
                txt += "Buzz";
            }
            if (txt.isEmpty()) {
                result.add(txt + i);
            } else {
                result.add(txt);
            }
        }
        return result;
    }
}
